package nio.udp;

import nio.tcp.server_client.Server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;

public class DatagramChannelSender {

    public static void main(String[] args) throws IOException {

        try (DatagramChannel sender = DatagramChannel.open()) {

            sender.configureBlocking(false);
//			sender.bind(new InetSocketAddress(1111));
            sender.connect(new InetSocketAddress("localhost", 2222));//似乎没用

            ByteBuffer bb = ByteBuffer.allocate(Server.buffer_size);
            bb.put("sender的数据".getBytes());
            bb.flip();
            while (bb.hasRemaining()) {
                sender.write(bb); //write和send一样
//				sender.send(bb, new InetSocketAddress("localhost", 2222));
                bb.flip();
                System.out.println(Charset.forName("utf-8").newDecoder().decode(bb));
            }
        }
    }
}
